﻿using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using TfsBugger.Util.Ui.Native;

namespace TfsBugger.Util.Ui
{
    public class BuggerPopup
    {
        public BuggerPopup(Form frm, int showAnimDuration)
        {
            m_frmToShow = frm;
            m_showAnimDuration = showAnimDuration;
        }


        #region Fields
        private Form m_frmToShow;
        private int m_timerTicks = 0;
        private int m_showAnimDuration = 0;
        #endregion 


        #region Helpers
        
        #endregion 


        #region Handlers for the Window
        
        #endregion


        #region Public Methods
        public void BeginShow()
        {
            //Get the position  of the taskbar first 
            APPBARDATA tskBarData = new APPBARDATA();
            tskBarData.cbSize = Marshal.SizeOf(tskBarData);
            tskBarData.hWnd = m_frmToShow.Handle;
            NativeMethods.SHAppBarMessage(NativeConstants.ABM_GETTASKBARPOS, ref tskBarData);

            m_frmToShow.Location = new Point(tskBarData.rc.right - 20 - m_frmToShow.Width, tskBarData.rc.top - 20 - m_frmToShow.Height);
            NativeMethods.ShowWindow(m_frmToShow.Handle, NativeConstants.SW_SHOWNOACTIVATE);
            Timer tmr = new Timer();
            tmr.Interval = 50;
            int maxTicks = (m_showAnimDuration * 1000) / tmr.Interval;
            m_frmToShow.AllowTransparency = true;
            double incr = 1D / (double)maxTicks;
            tmr.Tick += delegate(object sender, EventArgs e)
            {
                m_timerTicks++;
                if (m_timerTicks >= maxTicks)
                {
                    tmr.Stop();
                    tmr.Dispose();
                    m_frmToShow.Opacity = 1;
                    m_timerTicks = 0;
                    return;
                }

                m_frmToShow.Opacity += incr;
            };

            tmr.Start();
        }
        #endregion
    }
}
